/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import com.cleangwt.bootstrap.client.style.Panels;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HeaderPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A {@link HeaderPanel} with panel style.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class HeadingLayoutPanel extends HeaderPanel {

  /**
   * Creates a heading layout panel.
   * 
   */
  public HeadingLayoutPanel() {
    Element headerContainer = this.getElement().getChild(0).cast();
    Element footerContainer = this.getElement().getChild(1).cast();
    Element bodyContainer = this.getElement().getChild(2).cast();

    headerContainer.setClassName(Panels.HEADING_STYLE);
    footerContainer.setClassName(Panels.FOOTER_STYLE);
    bodyContainer.getStyle().setOverflowY(Overflow.AUTO);
    bodyContainer.getStyle().setOverflowX(Overflow.AUTO);
  }

  /**
   * Creates a heading layout panel with the given type style.
   * 
   * @param style the base style
   */
  public HeadingLayoutPanel(String style) {
    this();
    setStyleBase(style);
  }

  /**
   * Sets the caption for header.
   * 
   * @param caption the caption for header
   */
  public void setCaption(String caption) {
    HTML header = new HTML(caption);
    header.setStyleName(Panels.TITLE_STYLE);
    setHeaderWidget(header);
  }

  /**
   * Sets the content widget.
   * 
   * @param w the content widget
   */
  @Override
  public void setContentWidget(Widget w) {
    SimplePanel wrapper = new SimplePanel(w);
    wrapper.addStyleName(Panels.BODY_STYLE);
    super.setContentWidget(wrapper);
  }

  /**
   * Resets the style name with base panel style and the given type style.
   * 
   * @param style the base style
   */
  public void setStyleBase(String style) {
    assert style != null : "Stye must not be null.";
    setStyleName(Panels.PANEL_STYLE + " " + style);
  }

}
